//
//  JVGradeApplyVController.m
//  jiMuOperate
//
//  Created by 胡梅方 on 16/5/6.
//  Copyright © 2016年 jinXinFengLi. All rights reserved.
//

#import "JVGradeApplyVController.h"
#import "GradeApplyView.h"
#import "JMGraAplySucessVController.h"
@interface JVGradeApplyVController ()<UITextFieldDelegate>
@property(nonatomic,weak)UIButton *confirm;
@property(nonatomic,weak)GradeApplyView *gradeApplyView;
@property(nonatomic,weak)UILabel *titleLabel;
@property(nonatomic,weak)UILabel *firstTitlL;
@property(nonatomic,weak)UILabel *secondTitlL;
@property(nonatomic,weak)UILabel *thirdTitlL;
@property(nonatomic,weak)UserEntity *user;
@end

@implementation JVGradeApplyVController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"等级申请";
    [self createViews];
    self.gradeApplyView.buyNutNum.delegate = self;
    UserEntity *user = [UserEntity shardUserInfo];
    self.user = user;
    self.gradeApplyView.existNutsNum.text = self.user.PineConeGreen;
    self.gradeApplyView.curentLevelNum.text = self.user.UserLevel;
    
}
-(void)createViews
{
    self.view.backgroundColor = BACKCOLOR;
    CGFloat labelX = 15;
    CGFloat labelY = 320;
    CGFloat labelW = 320;
    CGFloat labelH = 20;
    GradeApplyView *gradeAppView = [[GradeApplyView alloc]initWithFrame:CGRectMake(0, 75, labelW, 235)];
    self.gradeApplyView = gradeAppView;
    gradeAppView.frame = [ToolViewAndData MyAutoLayout:gradeAppView];
    [self.view addSubview:gradeAppView];
    
    
    UILabel *titleLab = [ARC createLabelWithFrame:CGRectMake(labelX, labelY, 320, labelH) Font:BYPiFont-1 Text:@"绿松果使用条件:"];
    self.titleLabel = titleLab;
    [self.view addSubview:titleLab];
    
    
    UILabel *firstLab = [ARC createLabelWithFrame:CGRectMake(labelX, labelY+20, 305, labelH) Font:BYPiFont-2 Text:@"1.绿松果用于社员升级"];
    self.firstTitlL = firstLab;
    
    firstLab.textAlignment = NSTextAlignmentCenter;
    firstLab.lineBreakMode = NSLineBreakByWordWrapping;
    firstLab.numberOfLines = 0;
    [self.view addSubview:firstLab];
    
    
    UILabel *secondLab = [ARC createLabelWithFrame:CGRectMake(labelX, labelY+40, 320, 20) Font:BYPiFont-1 Text:@"2.红松果降级只能用未绑定的库存"];
    self.secondTitlL = secondLab;
    [self.view addSubview:secondLab];
    UILabel *thirdTitlL = [ARC createLabelWithFrame:CGRectMake(labelX, labelY+60, 320, 20) Font:BYPiFont-1 Text:@"2.红松果降级只能用未绑定的库存"];
    self.thirdTitlL = thirdTitlL;
    [self.view addSubview:thirdTitlL];
    self.titleLabel.textColor = [UIColor grayColor];
    self.firstTitlL.textColor = [UIColor grayColor];
    self.secondTitlL.textColor = [UIColor grayColor];
    self.thirdTitlL.textColor = [UIColor grayColor];
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.firstTitlL.textAlignment = NSTextAlignmentLeft;
    self.secondTitlL.textAlignment = NSTextAlignmentLeft;
    self.thirdTitlL.textAlignment = NSTextAlignmentLeft;
    self.titleLabel.frame =[ToolViewAndData MyAutoLayout:self.titleLabel];
    self.firstTitlL.frame =[ToolViewAndData MyAutoLayout:self.firstTitlL];
    self.secondTitlL.frame =[ToolViewAndData MyAutoLayout:self.secondTitlL];
    self.thirdTitlL.frame = [ToolViewAndData MyAutoLayout:self.thirdTitlL];
    
    UIButton *confirm = [ARC createButtonWithFrame:CGRectMake(20, 450, 280, 40) Target:self Sel:@selector(confirmBtnClick:)];
    self.confirm = confirm;
    confirm.frame = [ToolViewAndData MyAutoLayout:confirm];
    [confirm setBackgroundImage:[UIImage imageNamed:@"logoin_btn"] forState:UIControlStateNormal];
    [confirm setTitle:@"确认支付" forState:UIControlStateNormal];
    [self.view addSubview:confirm];

}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
   
    NSInteger greenNutNum = [self.gradeApplyView.buyNutNum.text intValue];
    if (greenNutNum>11) {
        greenNutNum=11;
        self.gradeApplyView.buyNutNum.text = @"11";
    }
    self.gradeApplyView.afbuyNutNum.text = [NSString stringWithFormat:@"%d",greenNutNum+[self.user.PineConeGreen integerValue]];

    if ([self.gradeApplyView.afbuyNutNum.text integerValue]>11) {
        self.gradeApplyView.afbuyNutNum.text = @"11";
    }
    self.gradeApplyView.finalLevelNum.text = [NSString stringWithFormat:@"%d",greenNutNum+[self.user.PineConeGreen integerValue]-1];
    if ([self.gradeApplyView.finalLevelNum.text integerValue]<0) {
        self.gradeApplyView.finalLevelNum.text = @"0";
    }
    self.gradeApplyView.totalNum.text =[NSString stringWithFormat:@"￥%d",greenNutNum*1000];
     [self.gradeApplyView.buyNutNum resignFirstResponder];
    return YES;
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSInteger greenNutNum = [self.gradeApplyView.buyNutNum.text intValue];
    if (greenNutNum>11) {
        greenNutNum=11;
        self.gradeApplyView.buyNutNum.text = @"11";
    }
    self.gradeApplyView.afbuyNutNum.text = [NSString stringWithFormat:@"%d",greenNutNum+[self.user.PineConeGreen integerValue]];

    if ([self.gradeApplyView.afbuyNutNum.text integerValue]>11) {
        self.gradeApplyView.afbuyNutNum.text = @"11";
    }
    self.gradeApplyView.finalLevelNum.text = [NSString stringWithFormat:@"%ld",greenNutNum+[self.user.PineConeGreen integerValue]-1];
    if ([self.gradeApplyView.finalLevelNum.text integerValue]<0) {
        self.gradeApplyView.finalLevelNum.text = @"0";
    }else if([self.gradeApplyView.finalLevelNum.text integerValue]>10){
        self.gradeApplyView.finalLevelNum.text = @"10";
    }
     self.gradeApplyView.totalNum.text =[NSString stringWithFormat:@"￥%ld",greenNutNum*1000];
        [self.gradeApplyView.buyNutNum resignFirstResponder];
}
-(void)confirmBtnClick:(UIButton*)btn
{
    NSString *token = self.user.access_token;

   
    [self requestRechareApplyWithToken:token PineNut:@"" PineConeGreen:self.gradeApplyView.buyNutNum.text PineConeRed:@"" StrategyPeopleId:@"" RequestNo:@""];
    
}

-(void)requestRechareApplyWithToken:(NSString*)token PineNut:(NSString*)pinenut PineConeGreen:(NSString*)pineConeGreen PineConeRed:(NSString*)pineConerRed StrategyPeopleId:(NSString*)stategyPeopleId RequestNo:(NSString*)requestNo
{
    NSString *urlRe = [NSString stringWithFormat:@"%@",JMURLRechareApply];
    NSDictionary *dictRe = [JMData Token:token PineNut:pinenut PineConeGreen:pineConeGreen PineConeRed:pineConerRed StrategyPeopleId:stategyPeopleId RequestNo:requestNo];
        NSString *strAry = [JMData stringWithDict:dictRe];
    NSArray *aryRe = @[strAry,urlRe];
    [Request startRequestWithArray:aryRe FinishedBlock:^(NSData *data) {
        NSDictionary *dict = [JMData dictAndTestData:data];
        NSLog(@"dsgffds%@",dict);
        
        
    } FailBlock:^(NSString *string) {
      [MBProgressHUD showError:@"登录失败，请梢后重试"];
    }];

}

@end
